package com.ffcs.crmd.tsp.task.netty.processor;

import org.slf4j.Logger;

import com.ffcs.crmd.tsp.api.dto.netty.RemotingCommand;
import com.ffcs.crmd.tsp.api.dto.task.TaskThreadPoolInfo;
import com.ffcs.crmd.tsp.api.netty.INettyProcessor;
import com.ffcs.crmd.tsp.api.netty.INettyRemotingClient;
import com.ffcs.crmd.tsp.common.constant.LoggerName;
import com.ffcs.crmd.tsp.core.util.XmlUtil;
import com.ffcs.crmd.tsp.task.container.ThreadPoolContainer;
import com.ffcs.crmd.tsp.task.log.TaskLogger;
import com.ffcs.crmd.tsp.task.threadpool.JobProcessBusinessThreadPool;
import com.ffcs.crmd.tsp.task.threadpool.JobProcessThreadPool;

import io.netty.channel.ChannelHandlerContext;

/**
 * 
 * 功能说明:线程池处理者
 *
 * @author ZHONGFUHUA
 * 
 * @since 1.0.0-SNAPSHOT
 *
 */
public class UpdateThreadPoolProcessor<T> implements INettyProcessor {
    
    /**
     * 日志
     */
    private static final Logger          LOG                          = TaskLogger
        .getLogger(LoggerName.TaskLoggerName);
    
    /**
     * 任务批量业务处理线程池
     */
    private JobProcessBusinessThreadPool jobProcessBusinessThreadPool = ThreadPoolContainer
        .getInstance().getJobProcessBusinessThreadPool();
    
    /**
     * 任务处理线程池
     */
    private JobProcessThreadPool         jobProcessPool               = ThreadPoolContainer
        .getInstance().getJobProcessThreadPool();
    
    /**
     * 功能说明:
     * 
     * @param nettyRemotingClient
     */
    public UpdateThreadPoolProcessor(INettyRemotingClient nettyRemotingClient) {
        
    }
    
    @Override
    public RemotingCommand processRequest(ChannelHandlerContext ctx, RemotingCommand request) {
        TaskThreadPoolInfo taskThreadPoolInfo = XmlUtil.fromXml(new String(request.getBody()), TaskThreadPoolInfo.class);
        
        LOG.info("TASK-NETTY: recive a request update job process business thread pool num to {}.", 
            taskThreadPoolInfo.getJobProcessBusinessThreadPoolSize());
        LOG.info("TASK-NETTY: recive a request update job process thread pool num to {}.", 
            taskThreadPoolInfo.getJobProcessThreadPoolSize());
        
        if (taskThreadPoolInfo.getJobProcessBusinessThreadPoolSize() != null && 
            jobProcessBusinessThreadPool.getPoolSize() != taskThreadPoolInfo.getJobProcessBusinessThreadPoolSize()) {
            jobProcessBusinessThreadPool.updatePoolSize(taskThreadPoolInfo.getJobProcessBusinessThreadPoolSize());
        }
        
        if (taskThreadPoolInfo.getJobProcessThreadPoolSize() != null 
            && jobProcessPool.getPoolSize() != taskThreadPoolInfo.getJobProcessThreadPoolSize()) {
            jobProcessPool.updatePoolSize(taskThreadPoolInfo.getJobProcessThreadPoolSize());
        }
        
        if(taskThreadPoolInfo.getJobProcessThreadPriorityWeight()!= null){
            jobProcessPool.updatePriority(taskThreadPoolInfo.getJobProcessThreadPriorityWeight());
        }
        
        if(taskThreadPoolInfo.getJobProcessBusinessThreadPriorityWeight()!= null){
            jobProcessBusinessThreadPool.updatePriority(taskThreadPoolInfo.getJobProcessBusinessThreadPriorityWeight());
        }
        return null;
    }
    
}
